package com.jojo.android.zxlib.base;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.tool.ZxLogTool;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseFragment<P extends BasePresenter> extends Fragment {
    private final String TAG = "BaseFragment";
    protected Context mContext;
    protected P presenter = null;
    protected View rootView;
    private boolean isNewViewCreated = true; //视图是否已创建
    private boolean visibleStatus = false; //fragment可见状态
    private Unbinder unbinder;
    private CharSequence title;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = initPresenter();
    }

    public CharSequence getTitle() {
        return title;
    }

    public void setTitle(CharSequence title) {
        this.title = title;
    }

    @Nullable
    @Override
    public Context getContext() {
        return mContext;
    }

    protected abstract P initPresenter();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //确保视图在创建后不会重复创建
        if (rootView == null) {
            isNewViewCreated = true;
            rootView = inflater.inflate(getLayoutId(), container, false);
        }
        return rootView;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        initView();
        if (isVisibleToUser != visibleStatus) {
            visibleStatus = isVisibleToUser;
            onUserVisibleChanged(isVisibleToUser);
        }

    }

    /**
     * 当视图在可见且视图第一次创建时才去初始化UI和加载数据
     */
    private synchronized void initView() {
        if (isNewViewCreated && getUserVisibleHint() && rootView != null) {
            try {
                unbinder = ButterKnife.bind(this, rootView);
                initUI();
                initEvent();
                initData();
                isNewViewCreated = false;
            } catch (Exception e) {
                ZxLogTool.err(e);
            }

        }
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    public void onUserVisibleChanged(boolean visible) {

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
        if (presenter != null) {
            presenter.destroy();
        }
        if (null != unbinder)
            unbinder.unbind();
        rootView = null;
    }


    protected abstract int getLayoutId();

    protected abstract void initUI();

    protected abstract void initEvent();

    protected abstract void initData();
}
